/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package modele;

import android.graphics.drawable.Drawable;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 *
 * @author plasalmonie
 */
public class DAO {

    public static List<String> getLesDeps() {
        List<String> lesDeps = new ArrayList<String>();
        String url = "http://gaemedecins.appspot.com/Controleur/medParDep/listeDep";
        try {
            URL myURL = new URL(url);
            Document doc;
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            doc = db.parse(myURL.openStream());
            Element racine = doc.getDocumentElement();
            NodeList listeDep = racine.getElementsByTagName("Departement");
            //recup des deps
            for (int i = 0; i < listeDep.getLength(); i++) {
                Node dep = listeDep.item(i);
                NodeList lesProprietes = dep.getChildNodes();
                //recherche du num
                for (int j = 0; j < lesProprietes.getLength(); j++) {
                    if (lesProprietes.item(j).getNodeName().equals("num")) {
                        lesDeps.add(lesProprietes.item(j).getTextContent().trim());
                        break;
                    }
                }
            }
        } catch (Exception e) {
            Logger.getLogger(DAO.class.getName()).log(Level.SEVERE, null, e);
        }
        return lesDeps;
    }

    public static List<Med> getLesMeds(String num) {
        List<Med> lesMeds = new ArrayList<Med>();
        String url = "http://gaemedecins.appspot.com/Controleur/medParDep/listeMed/" + num + "";
        try {
            String leNom = "", lePrenom = "", adresse = "", laSpe = "", leTel = "";
            URL myURL = new URL(url);
            Document doc;
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            doc = db.parse(myURL.openStream());
            org.w3c.dom.Element racine = doc.getDocumentElement();
            NodeList listeDep = racine.getElementsByTagName("Medecin");
            //recup des med
            for (int i = 0; i < listeDep.getLength(); i++) {
                Node dep = listeDep.item(i);
                NodeList lesProprietes = dep.getChildNodes();
                for (int j = 0; j < lesProprietes.getLength(); j++) {
                    if (lesProprietes.item(j).getNodeName().equals("nom")) {
                        leNom = lesProprietes.item(j).getTextContent().trim();
                    }
                    if (lesProprietes.item(j).getNodeName().equals("prenom")) {
                        lePrenom = lesProprietes.item(j).getTextContent().trim();
                    }
                    if (lesProprietes.item(j).getNodeName().equals("adresse")) {
                        adresse = lesProprietes.item(j).getTextContent().trim();
                    }
                    if (lesProprietes.item(j).getNodeName().equals("specialite")) {
                        laSpe = lesProprietes.item(j).getTextContent().trim();
                    }
                    if (lesProprietes.item(j).getNodeName().equals("tel")) {
                        leTel = lesProprietes.item(j).getTextContent().trim();
                    }
                }
                lesMeds.add(new Med(leNom.toUpperCase(), lePrenom, adresse, leTel, laSpe));
            }
        } catch (Exception e) {
            Logger.getLogger(DAO.class.getName()).log(Level.SEVERE, null, e);
        }
        return lesMeds;
    }
        public static Drawable LoadImageFromWebOperations(String url) {
        try {
            InputStream is = (InputStream) new URL(url).getContent();
            Drawable d = Drawable.createFromStream(is, null);
            return d;
        } catch (Exception e) {
            return null;
        }
    }
}
